//
//  FilesViewController.m
//  History
//
//  Created by jam on 16/9/5.
//  Copyright © 2016年 kane. All rights reserved.
//

#import "FilesViewController.h"
#import "SubjectSearchViewController.h"

@interface FilesViewController ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>
{
    __weak IBOutlet UITextField *fileNameField;
    __weak IBOutlet UITableView *channelTable;
    __weak IBOutlet UILabel *titleLab;
    __weak IBOutlet UIView *channelView;
    __weak IBOutlet UIScrollView *scrollView;
    
    NSMutableArray *dataList;
    
    
}

@end

@implementation FilesViewController

@synthesize nameList;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    self.navigationController.navigationBarHidden = YES;
    scrollView.contentSize = CGSizeMake(self.view.width, 1000);
    [self loadData];
    channelTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeNightMode) name:kChangeNightMode object:nil];
    [self changeNightMode];
}

- (void)changeNightMode
{
    titleLab.textColor = KKColor_White_Black;
}

-(void)loadData
{
    [dataList removeAllObjects];
    dataList = [NSMutableArray arrayWithArray:nameList];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark--tableviewDataSource&&delegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section ==0) {
        UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 25)];
        headView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        UILabel *searchLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 150, 20)];
        searchLabel.text = @"从已订频道中选择";
        searchLabel.font = [UIFont boldSystemFontOfSize:15];
        searchLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
        [headView addSubview:searchLabel];
        return headView;
    }else return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 25;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  dataList.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        NSString *identifier = @"FileCell";
        UITableViewCell *cell = [channelTable dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.textLabel.text = dataList[indexPath.row][@"channel_name"];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0, 50, 50);
        [btn setImage:[UIImage imageNamed:@"orginal"] forState:UIControlStateNormal];
        [btn setImageEdgeInsets:UIEdgeInsetsMake(12, 12, 12, 12)];
        btn.tag = 222+indexPath.row;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        cell.accessoryView = btn;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
//    }
//    return nil;
}



-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [fileNameField resignFirstResponder];
}


#pragma mark--userAction


- (IBAction)searchBtnClick:(UIButton *)sender {
    SubjectSearchViewController *subVC = [[SubjectSearchViewController alloc] init];
    [self.navigationController pushViewController:subVC animated:YES];
}


-(void)btnClick:(UIButton *)sender
{
    sender.selected = !sender.selected;
    if (sender.selected) {
        [sender setImage:[UIImage imageNamed:@"gou2"] forState:UIControlStateNormal];
    }
    else {
        [sender setImage:[UIImage imageNamed:@"orginal"] forState:UIControlStateNormal];
    }
    
}




- (IBAction)completeBtnClick:(UIButton *)sender {
    
}


- (IBAction)backBtnClick:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}



@end
